package com.srd.phone.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.Paint;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.style.AbsoluteSizeSpan;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.shengrongdai.srd.R;
import com.srd.phone.activity.TenderDetailActivity;
import com.srd.phone.bean.TenderInfo;
import com.srd.phone.utils.views.RoundProgressBar;
import com.srd.phone.utils.views.ViewHolder;

import java.util.ArrayList;
import java.util.List;

public class TabTenderListZqAdapter extends BaseAdapter {

    Context context;
    List<TenderInfo> list = new ArrayList<>();

    public TabTenderListZqAdapter(Context context) {
        this.context = context;
    }

    public void setData(List<TenderInfo> list0) {
        list = list0;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        TenderInfo info = list.get(position);
        ViewHolder viewHolder = ViewHolder.get(context, convertView, parent, R.layout.fragment_tender_zq_item, position);
        TextView tv_tender_zq = viewHolder.getView(R.id.tv_tender_zq);
        tv_tender_zq.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG);
        tv_tender_zq.getPaint().setAntiAlias(true);
        tv_tender_zq.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                context.startActivity(new Intent(context, TenderDetailActivity.class));
            }
        });
        TextView tv_tender_zq_lilv = viewHolder.getView(R.id.tv_tender_zq_lilv); // 年利率
        TextView tv_tender_zq_time = viewHolder.getView(R.id.tv_tender_zq_time); //满标用时
        TextView tv_tender_zq_money = viewHolder.getView(R.id.tv_tender_zq_money); //投资金额
        TextView tv_tender_zq_peoples = viewHolder.getView(R.id.tv_tender_zq_peoples); //投资人数
        TextView tv_tender_zq_every_people = viewHolder.getView(R.id.tv_tender_zq_every_people); //平均人投资
        RoundProgressBar rpb_tender_zq = viewHolder.getView(R.id.rpb_tender_zq); //进度条
        TextView tv_tender_zq_jingpai = viewHolder.getView(R.id.tv_tender_zq_jingpai);
        if(position == 0) {
            rpb_tender_zq.setProgress(100);
            rpb_tender_zq.setStyleValue(1);
            tv_tender_zq_jingpai.setText("立即竞拍");
        }else{
            rpb_tender_zq.setProgress(0);
            rpb_tender_zq.setStyleValue(1);
            tv_tender_zq_jingpai.setText("已结束");
        }
        setTenderTextSpan(tv_tender_zq_lilv, "12.8");
        return viewHolder.getConvertView();
    }

    /**
     * 标的数据赋值
     */
    public void setTenderTextSpan(TextView tv, String... strs) {
        String value = strs[0];
        SpannableString sb = new SpannableString(value);
        sb.setSpan(new AbsoluteSizeSpan(20, true), 0, value.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
        sb.setSpan(new ForegroundColorSpan(Color.parseColor("#bf1829")), 0, value.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
        tv.setText(sb);
        value = strs[1];
        sb = new SpannableString(value);
        sb.setSpan(new AbsoluteSizeSpan(12, true), 0, value.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
        sb.setSpan(new ForegroundColorSpan(Color.parseColor("#bf1829")), 0, value.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
        tv.append(sb);
        if (TextUtils.isEmpty(strs[2])) {
            return;
        }
        value = "\n" + strs[2];
        sb = new SpannableString(value);
        sb.setSpan(new AbsoluteSizeSpan(11, true), 0, value.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
        sb.setSpan(new ForegroundColorSpan(Color.BLACK), 0, value.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
        tv.append(sb);
    }

    /**
     * 标的数据赋值
     */
    public void setTenderTextSpan(TextView tv, String strs) {
        String value = strs;
        SpannableString sb = new SpannableString(value);
        sb.setSpan(new AbsoluteSizeSpan(20, true), 0, value.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
        sb.setSpan(new ForegroundColorSpan(Color.parseColor("#bf1829")), 0, value.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
        tv.setText(sb);
        value = "%";
        sb = new SpannableString(value);
        sb.setSpan(new AbsoluteSizeSpan(10, true), 0, value.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
        sb.setSpan(new ForegroundColorSpan(Color.parseColor("#bf1829")), 0, value.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
        tv.append(sb);
    }
}
